
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct _stack_t
{
    int *pBottom;
    int *pTop;
    int len;
} stack_t;
#define STACK_LEN 10

int initStack(stack_t *pStack, int len)
{
    pStack->len = len;
    pStack->pBottom = pStack->pTop = (int *)malloc(pStack->len * sizeof(int));

    return 0;
}

int pushStack(stack_t *pStack, int data)
{
    if (pStack->pTop >= (pStack->pBottom + pStack->len)) {
        return -1;
    }

    *(pStack->pTop) = data;
    pStack->pTop++;

    return 0;
}

int popStack(stack_t *pStack, int *data)
{
    if (pStack->pTop == pStack->pBottom){
        return -1;
    }

    pStack->pTop--;
    *data = *(pStack->pTop);

    return 0;
}

void deinit(stack_t *pStack)
{
    if (pStack->pBottom != NULL) {
        free(pStack->pBottom);
        pStack->pBottom = NULL;
    }
}

int main(void)
{
    int ret = 0;
    stack_t stack = {0};

    initStack(&stack, STACK_LEN);


    for (int i = 0; i < 13; i++) {
        ret = pushStack(&stack, i);
        if (ret != 0) {
            printf("push data %d error \n", i);
        }
    }

    for(int i = 0; i < 13; i++) {
        int data = -1;
        ret = popStack(&stack, &data);
        printf("pop data %d i %d\n", data, i);
    }

    deinit(&stack);

    return 0;
}